package org.PISS.DAO;

import org.PISS.utils.HibernateUtil;
import org.hibernate.Session;

import java.util.List;

/**
 * Created by admin on 12.04.14.
 */
class DAOUtils {
    static void add(Object o){
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(o);
            session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException("Save exception",e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    static void add(List list){
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            if (list!=null&&!list.isEmpty()){
                for(Object o:list)
                    session.save(o);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException("Save exception",e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
